Imports System.Collections

Partial Public Class pages_Sets_usercontrols_UCSets : Inherits UserControl

#Region "Events"
    Public Event AddClick()
    Public Event ResortClick(ByVal sortBy As String, ByVal descending As Boolean, ByVal pageNumber As Integer)
#End Region

#Region "Members"
    Private m_parent As CProduct
#End Region


#Region "Interface"
    Public Sub Display(parent As CProduct) 'Full list, paging done internally
        m_parent = parent

        For Each i As CProductType In parent.ProductType.ChildTypes
            UCType(plh).Display(i, parent)
        Next

        ddSermons.DataTextField = "ProductName"
        ddSermons.DataValueField = "ProductId"
        ddSermons.DataSource = CProduct.Cache.Sermons.GetByBelongsToProductId(Integer.MinValue)
        ddSermons.DataBind()
        CDropdown.BlankItem(ddSermons, "-- Add an Existing Sermon --")

        litTotal.Text = CTextbox.SetMoney(parent.Children.Total)
        If Not IsNothing(parent.Button) Then
            lblPrice.Text = CTextbox.SetMoney(parent.Button.ButtonPrice)
            lblSavings.Text = CTextbox.SetMoney(parent.Children.Total - parent.Button.ButtonPrice)
        End If
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCType(ByVal target As Control) As pages_products_usercontrols_UCType
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.UCType)
        target.Controls.Add(ctrl)
        Return CType(ctrl, pages_products_usercontrols_UCType)
    End Function
#End Region

    Protected Sub ddSermons_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSermons.SelectedIndexChanged
        With CProduct.Cache.GetById(CDropdown.GetInt(ddSermons))
            .ProductBelongsToProductId = m_parent.ProductId
            .ProductBelongsToSortOrder = m_parent.Children.Count
            .Save()
        End With
        Response.Redirect(Request.RawUrl)
    End Sub
End Class
